﻿using Koala.Pro.Common;
using Koala.Pro.Core;
using Koala.Pro.Core.Dto;
using Koala.Pro.Core.Models;
using SqlSugar;

namespace Koala.Pro.Services
{
    public class LoginLogService : MyCrudService<LoginLogInfo, int, LoginLogPagedDto>, ILoginLogService, IMyCrudService<LoginLogInfo, int, LoginLogPagedDto>, ITransientDependency, IDependency
    {
        public LoginLogService(IApiUserSession currentApiUser)
        {
            base.CurrentApiUser = currentApiUser;
        }

        public override Task<Dictionary<string, string>> GetColumnNameAlias()
        {
            return Task.FromResult(new Dictionary<string, string>
        {
            { "User_ID", "登录用户ID" },
            { "LoginName", "登录名" },
            { "FullName", "真实名称" },
            { "Company_ID", "所属公司ID" },
            { "CompanyName", "所属公司名称" },
            { "Note", "日志描述" },
            { "IPAddress", "IP地址" },
            { "MacAddress", "Mac地址" },
            { "LastUpdated", "更新时间" },
            { "SystemType_ID", "系统编号" }
        });
        }

        protected override ISugarQueryable<LoginLogInfo> CreateFilteredQueryAsync(LoginLogPagedDto input)
        {
            return base.CreateFilteredQueryAsync(input).WhereIF(input.ExcludeId.HasValue, (LoginLogInfo t) => (int?)t.Id != input.ExcludeId).WhereIF(!input.User_ID.IsNullOrWhiteSpace(), (LoginLogInfo t) => t.User_ID.Contains(input.User_ID))
                .WhereIF(!input.LoginName.IsNullOrWhiteSpace(), (LoginLogInfo t) => t.LoginName.Contains(input.LoginName))
                .WhereIF(!input.FullName.IsNullOrWhiteSpace(), (LoginLogInfo t) => t.FullName.Contains(input.FullName))
                .WhereIF(!input.Company_ID.IsNullOrWhiteSpace(), (LoginLogInfo t) => t.Company_ID.Equals(input.Company_ID))
                .WhereIF(!input.CompanyName.IsNullOrWhiteSpace(), (LoginLogInfo t) => t.CompanyName.Contains(input.CompanyName))
                .WhereIF(!input.Note.IsNullOrWhiteSpace(), (LoginLogInfo t) => t.Note.Contains(input.Note))
                .WhereIF(!input.IPAddress.IsNullOrWhiteSpace(), (LoginLogInfo t) => t.IPAddress.Contains(input.IPAddress))
                .WhereIF(!input.MacAddress.IsNullOrWhiteSpace(), (LoginLogInfo t) => t.MacAddress.Contains(input.MacAddress))
                .WhereIF(input.LastUpdatedStart.HasValue, (LoginLogInfo s) => s.LastUpdated >= input.LastUpdatedStart.Value)
                .WhereIF(input.LastUpdatedEnd.HasValue, (LoginLogInfo s) => s.LastUpdated <= input.LastUpdatedEnd.Value)
                .WhereIF(!input.SystemType_ID.IsNullOrWhiteSpace(), (LoginLogInfo t) => t.SystemType_ID.Contains(input.SystemType_ID));
        }

        protected override ISugarQueryable<LoginLogInfo> ApplyDefaultSorting(ISugarQueryable<LoginLogInfo> query)
        {
            return query.OrderBy((LoginLogInfo s) => s.LastUpdated, OrderByType.Desc);
        }

        public async Task<bool> AddLoginLog(UserInfo userInfo, string systemType, string note, string ip = null, string macAddr = null)
        {
            LoginLogInfo loginLogInfo = new LoginLogInfo();
            if (userInfo != null)
            {
                loginLogInfo.User_ID = userInfo.Id.ToString();
                loginLogInfo.LoginName = userInfo.Name;
                loginLogInfo.FullName = userInfo.FullName;
                loginLogInfo.Company_ID = userInfo.Company_ID;
                loginLogInfo.CompanyName = userInfo.CompanyName;
            }
            loginLogInfo.IPAddress = ip;
            loginLogInfo.MacAddress = macAddr;
            loginLogInfo.Note = note;
            loginLogInfo.SystemType_ID = systemType;
            loginLogInfo.LastUpdated = DateTime.Now;
            return await InsertAsync(loginLogInfo);
        }

        public async Task<LoginLogInfo> GetLastLoginInfo(string userId)
        {
            ISugarQueryable<LoginLogInfo> sugarQueryable = (from s in base.EntityDb.AsQueryable()
                                                            where s.User_ID == userId
                                                            select s).OrderBy((LoginLogInfo s) => s.LastUpdated, OrderByType.Desc).Take(2);
            if (sugarQueryable.Count() == 2)
            {
                return await sugarQueryable.FirstAsync();
            }
            return null;
        }

        public async Task<bool> DeleteMonthLog()
        {
            return await DeleteAsync((LoginLogInfo s) => s.LastUpdated < DateTime.Now.AddMonths(-1));
        }
    }
}
